<?php
/**
 * CreateTime: 2022/5/30 15:36
 */

namespace App\Models\LendingModel;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\ContractConsts\ContractStatusConsts;
use App\Consts\TableConsts\TableConsts;
use App\Models\BaseModel;
use App\Models\NewContract\ContractModel;

class LendingModel extends BaseModel
{
    protected $table = TableConsts::CY_LENDING;

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->m_cHelper = app()->gcHelper;
        $this->cDb = app()->gcHelper->gfDbQuery();
    }

    public $timestamps = false;

    public function getTableName(){ return $this->m_cHelper->gfGetTableName($this->table)	; }

    public function addLending($lendingData){
        return $this->insertGetId($lendingData);
    }

    public function getLendingInfoByLendingId($lendingId,$select = ["*"]){
        return $this
            ->select($select)
            ->where('lending_id',$lendingId)
            ->find();
    }

    public function getLendingListByStaffId($staffId,$select = ["*"]){
        return $this
            ->select($select)
            ->where('lending_staff_id',$staffId)
            ->get();
    }

    public function editLending($lendingId,$lendingData){

    }

    public function getLendingInfo($lendingId,$select = ["*"])
    {
        return $this->select($select)
            ->where('lending_id',$lendingId)
            ->first();
    }

    public function getLendingList($query, $pageSize,$page)
    {

        return $query
            ->withoutGlobalScopes()
            ->latest('lending_id')
            ->Paginate($pageSize,$page);
    }
	public function archivesBorrowList($j){
		
		#
		#
		$this->m_cHelper->gfDbQuery()->select("
			
				select 
						a.* 
						,b.*
						,b.responsible_staff_id																as archives_origin_holder_id
						,c.staff_name																		as archives_origin_holder_name
						,b.responsible_department_id														as archives_origin_holder_department_id
						,e.department_id																	as receive_department_id
						,e.department_name																	as receive_department_name
				from 
						".$this->m_cHelper->gfGetTableNameByConstKey('CY_LENDING_TABLE_NAME')."				as a
				left join
						".$this->m_cHelper->gfGetTableNameByConstKey('CY_CONTRACT_TABLE_NAME')."			as b
				on
						a.contract_id			= b.contract_id
				left join
						".$this->m_cHelper->gfGetTableNameByConstKey('CY_STAFF_TABLE_NAME')."				as c
				on
						b.responsible_staff_id	= c.staff_id					
				left join
						".$this->m_cHelper->gfGetTableNameByConstKey('CY_STAFF_DEPARTMENT_TABLE_NAME')."	as d
				on
						c.staff_id				= d.staff_id
				left join
						".$this->m_cHelper->gfGetTableNameByConstKey('CY_DEPARTMENT_TABLE_NAME')."			as e
				on
						e.department_id			= b.record_department_id	/* 归档部门id */
				where 
						a.lending_staff_id		= ?
				order by 
						a.lending_id desc
			",[$j->staff_id]
			,$j->page_size
			,$j->page
			,debug:false
		)->getPageOffsetInfo($ar)->forEachRow(function($row){
			
			return $row->appendItem([
				
								'contract_id'							=> $row->contract_id,
								'contract_code'							=> $row->contract_code,
								'contract_name'							=> $row->contract_name,
								'contract_relative_name'				=> $row->contract_name,
								'contract_start_time'					=> $row->contract_start_time,
								'archives_type_id'						=> $row->lending_type,
								'archives_type_name'					=> $this->m_cHelper->gfDbQuery()->getDbValue("
									
									select 
											dictionary_name  
									from 
											".$this->m_cHelper->gfGetTableNameByConstKey('CY_DATA_DICTIONARY_TABLE_NAME')." 
									where 
											dictionary_key in('lending_type_1','lending_type_2')
											and dictionary_value=?
								"
								,
								[$row->lending_type]
								),
								'archives_origin_holder_id'				=> $row->archives_origin_holder_id,
								'archives_origin_holder_name'			=> $row->archives_origin_holder_name,
								'archives_origin_holder_department_id'	=> $row->archives_origin_holder_department_id,
								'archives_status'						=> $row->lending_status,
								'receive_department_id'					=> $row->record_department_id,
								'receive_department_name'				=> $row->receive_department_name,
								'receive_id'							=> $row->record_owner_id,
								'receive_name'							=> $this->m_cHelper->gfDbQuery()->getDbValue("select staff_name from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_STAFF_TABLE_NAME')." where staff_id=?",[$row->record_owner_id]),
			])->getOneRowAllData();
		},$ar)->formatPagingStyle($ar);
		
		return $ar;
	}


    public function getDataBankList($page){
       $cm =  new ContractModel();
       $status = app()
           ->gcHelper
           ->getDictionaryInfoByDictionaryKey(ContractStatusConsts::CONTRACT_STATUS_RECORD,[
               'dictionary_value'
       ])['dictionary_value'];
       return $cm->where('contract_status',$status)
           ->where('status',BaseConsts::STATUS_NORMAL)
           ->get();
    }

	/*  获取所有归档的合同
	 *
	 */
	public function getLendingListEx($j){
	
		#
		#
		$this->m_cHelper->gfDbQuery()->select("
			
				select 
						a.* 
						,b.*
						,b.responsible_staff_id																as archives_origin_holder_id
						,c.staff_name																		as archives_origin_holder_name
						,b.responsible_department_id														as archives_origin_holder_department_id
						,e.department_id																	as receive_department_id
						,e.department_name																	as receive_department_name
				from 
						".$this->m_cHelper->gfGetTableNameByConstKey('CY_LENDING_TABLE_NAME')."				as a
				left join
						".$this->m_cHelper->gfGetTableNameByConstKey('CY_CONTRACT_TABLE_NAME')."			as b
				on
						a.contract_id			= b.contract_id
				left join
						".$this->m_cHelper->gfGetTableNameByConstKey('CY_STAFF_TABLE_NAME')."				as c
				on
						b.responsible_staff_id	= c.staff_id					
				left join
						".$this->m_cHelper->gfGetTableNameByConstKey('CY_STAFF_DEPARTMENT_TABLE_NAME')."	as d
				on
						c.staff_id				= d.staff_id
				left join
						".$this->m_cHelper->gfGetTableNameByConstKey('CY_DEPARTMENT_TABLE_NAME')."			as e
				on
						e.department_id			= b.record_department_id	/* 归档部门id */
				order by 
						a.lending_id desc
			",[]
			,$j->page_size
			,$j->page
			,debug:false
		)->getPageOffsetInfo($ar)->forEachRow(function($row){
			
			return $row->appendItem([
				
								'contract_id'							=> $row->contract_id,
								'contract_code'							=> $row->contract_code,
								'contract_name'							=> $row->contract_name,
								'contract_relative_name'				=> $row->contract_name,
								'contract_start_time'					=> $row->contract_start_time,
								'archives_type_id'						=> $row->lending_type,
								'archives_type_name'					=> $this->m_cHelper->gfDbQuery()->getDbValue("
									
									select 
											dictionary_name  
									from 
											".$this->m_cHelper->gfGetTableNameByConstKey('CY_DATA_DICTIONARY_TABLE_NAME')." 
									where 
											dictionary_key in('lending_type_1','lending_type_2')
											and dictionary_value=?
								"
								,
								[$row->lending_type]
								),
								'archives_origin_holder_id'				=> $row->archives_origin_holder_id,
								'archives_origin_holder_name'			=> $row->archives_origin_holder_name,
								'archives_origin_holder_department_id'	=> $row->archives_origin_holder_department_id,
								'archives_status'						=> $row->lending_status,
								'receive_department_id'					=> $row->record_department_id,
								'receive_department_name'				=> $row->receive_department_name,
								'receive_id'							=> $row->record_owner_id,
								'receive_name'							=> $this->m_cHelper->gfDbQuery()->getDbValue("select staff_name from ".$this->m_cHelper->gfGetTableNameByConstKey('CY_STAFF_TABLE_NAME')." where staff_id=?",[$row->record_owner_id]),
			])->getOneRowAllData();
		},$ar)->formatPagingStyle($ar);
		
		return $ar;
	}

    public function lendingRecord($query,$pageSize)
    {
        return $query->latest()->Paginate($pageSize);
    }

}